<?php
namespace app\single\controller;
class Index extends Common{
    /**
     * 基本的公共信息
     * @return [type] [description]
     */
    function index(){
        $uid=$this->user['id'];
        
        // 用户领取奖励百分比
        $map=[
            'id'=>$uid,
        ];
        $field='id,coin_expire,coin,vip_expire,sex,vocation,province,city,name,head_img,wxId,money,alipay,alipay_realname,weixin_realname,weixin,bank_realname,bank_name,bank_no,vip_id,vip_ratio,vip_level,is_agent,realName,is_certification,cert_remark,vip_trial,trial_member';
        $user=model('member')->read($map,$field,['with'=>'attr'])->append(['vip_text']);
        if($user['vip_expire']<=time()){ //会员VIP过期
            model('member')->isVip($uid);
            $user=model('member')->read($map,$field,['with'=>'attr'])->append(['vip_text']);
        }
        $attr=[];
        foreach ($user['attr'] as $v) {
            $attr[]=$v['name'];
        }
        $w=getWeek();
        $weekTime=$w[0];
        $weekEndTime=$w[1];

        $domain=request()->domain();
        $maxVipMoney=model('vip')->where(['status'=>1])->max('money');
        $vip_trial_id=0;
        if($user['vip_trial']==0&&$user['vip_level']==0){
            $vip_trial_id=7;
        }
        $rs=[
            'vip_trial_id'=>$vip_trial_id,
            'coin'=>$user['coin_expire']>time()?99999:$user['coin'],
            'vip_expire'=>$user['vip_expire'],
            'money'=>$user['money'],
            'is_agent'=>$user['is_agent'],
            'vip_level'=>$user['vip_level'],
            'vip_text'=>$user['vip_text'],
            'head_img'=>images($user['head_img']),
            'wxId'=>$user['wxId'],
            'name'=>$user['name'],
            'sex'=>$user['sex'],
            'vocation'=>$user['vocation'],
            'province'=>$user['province'],
            'city'=>$user['city'],
            'attr'=>implode(' ',$attr),
            'trial_member'=>$user['trial_member'],

            'realName'=>$user['realName'],
            'is_certification'=>10,//$user['is_certification'],
            'cert_remark'=>$user['cert_remark'],
            
            'alipay'=>$user['alipay'],
            'alipay_realname'=>$user['alipay_realname'],
            'weixin'=>$user['weixin'],
            'weixin_realname'=>$user['weixin_realname'],
            'bank_name'=>$user['bank_name'],
            'bank_no'=>$user['bank_no'],
            'bank_realname'=>$user['bank_realname'],
            'ratio'=>$user['vip_ratio'],
            'week'=>[date('W'),date('m.d',$weekTime).'-'.date('m.d',$weekEndTime-1)], //本周是第几周
            'week_bonus'=>config('setting.week_bonus'), //周奖金
            'week_standard'=>config('setting.week_standard'), //周达标金额（达标后可领奖金 ，week_bonus）

            'max_vip_money'=>$maxVipMoney,
            'feature'=>$domain.'/tagsicon/feature.jpg', 
            'zhuanqian_icon1'=>$domain.'/tagsicon/zhuanqian_icon1.png', 
            'zhuanqian_icon3'=>$domain.'/tagsicon/zhuanqian_icon3.png', 
        ];
        // 最高领取奖励百分比
        $rs['max_ratio']=model('vip')->where(['status'=>1])->max('ratio');
        // 我邀请的人数
        $rs['children_count']=model('member')->where(['pid'=>$uid])->count();
        // 过期未领取的金额(与用户比例的乘积即为用户已损失金额)
        $map=[
            'uid'=>$uid,
            'state'=>1,
            'is_gain'=>0,
            'expire_time'=>['expire_time','<',time()],
        ];
        $expire_money=model('order')->where($map)->sum('money');

        // 损失的未领取金额
        if($rs['ratio']>0){
            $rs['expire_money']=$expire_money*$rs['ratio']/100;
        }else{
            $rs['expire_money']=$expire_money*$rs['max_ratio']/100;
        }
        // 本周收入
        $map=[
            'uid'=>$uid,
            's_time'=>['s_time','between',[$weekTime,$weekEndTime]],
        ];
        $rs['week_money']=model('reward')->where($map)->sum('money');
        $rs['bonus']=model('reward')->where(['uid'=>$uid])->sum('money');
        // 本周已经领取周奖励次数
        $map['type']=1;
        $rs['week_gain_count']=model('reward')->where($map)->count();

        return json('',0,$rs);
    }
    /**
     * 生成分享图片
     * @return [type] [description]
     */
    function share(){
        $uid=$this->user['id'];
        $tel=$this->user['tel'];
        $tel=preg_replace('/^(\d{3})\d+(\d{4})$/',"\$1****\$2", $tel);
        $basemapDir=env('root_path').'public/basemap/';
        $files=read_files($basemapDir);
        $rs=[];
        $shareDir=env('root_path').'public/shareqrcode/';
        $domain=request()->domain();
        $domain_md5=md5($domain);
        $watermark=$shareDir.$domain_md5.$uid.'_qrcode.png';
        if(!is_file($watermark)){
            include env('root_path').'extend/phpqrcode.php';
            $value=url('index/single/share',['id'=>$uid],'html',$domain);
            \QRcode::png($value, $watermark, 'H', 7, 2);
        }
        
        foreach ($files['file'] as $v) {
            $qr=$uid.'_'.$v;
            if(!is_file($shareDir.$qr)){
                $file=explode('_', preg_replace("/\.[A-Za-z]+$/",'', $v));
                imgCopy($basemapDir.$v,$watermark,$shareDir.$qr,[
                    'marginLeft'=>$file[1],
                    'marginTop'=>$file[2],
                    'text'=>isset($file[3])?$tel:null,
                    'text_l'=>isset($file[3])?$file[3]:null,
                    'text_t'=>isset($file[4])?$file[4]:null,
                ]);
            }
            $rs[]=$domain.'/shareqrcode/'.$qr;
        }
        return json('',0,$rs);
    }
}